﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using WcfClientGenerator.Model.Common;
using System.ComponentModel;

namespace WcfClientGenerator.Model.Config
{
    /// <summary>
    /// The add-in configuration data
    /// </summary>
    [XmlRoot("wcfClientGeneratorConfig")]
    public class WcfClientGeneratorConfig : INotifyPropertyChanged
    {
        #region Constants (Private)

        private const string CONFIG_FILE_NAME = "WcfClientGeneratorConfig.v2.config";

        #endregion

        #region Events (Public)

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Instance Fields (Private)

        /// <summary>
        /// The collection of client proxy generation configurations 
        /// </summary>
        private List<WcfClientConfiguration> clientConfigurations = new List<WcfClientConfiguration>();
        /// <summary>
        /// The last selected configuration
        /// </summary>
        private int currentConfiguration = -1;
        /// <summary>
        /// Contains the communication parameters used when calling a generator service
        /// </summary>
        private GeneratorServiceConfiguration generatorServiceConfiguration = new GeneratorServiceConfiguration();

        #endregion

        #region Class Properties (Public)

        /// <summary>
        /// Gets the instance of the configuration file
        /// </summary>
        public static WcfClientGeneratorConfig Instance
        {
            get
            {
                WcfClientGeneratorConfig config = null;
                string configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_FILE_NAME);
                if (!File.Exists(configPath))
                    config = new WcfClientGeneratorConfig();
                else
                    config = (WcfClientGeneratorConfig)SerializationManager.XmlDeserialize(configPath, typeof(WcfClientGeneratorConfig));
                return config;
            }
        }

        #endregion

        #region Instance Properties (Public)

        /// <summary>
        /// The collection of client proxy generation configurations
        /// </summary>
        //[XmlArray("clientConfigurations", ElementName = "clientConfiguration")]
        [XmlArray(ElementName="clientConfigurations")]
        [XmlArrayItem("clientConfiguration")]
        public List<WcfClientConfiguration> ClientConfigurations
        {
            get
            {
                return this.clientConfigurations;
            }
            set
            {
                this.clientConfigurations = value;
            }
        }

        /// <summary>
        /// The last selected configuration
        /// </summary>
        [XmlElement("currentConfiguration")]
        public int CurrentConfiguration
        {
            get
            {
                return this.currentConfiguration;
            }
            set
            {
                int oldCurrentConfiguration = this.currentConfiguration;
                this.currentConfiguration = value;
                if (oldCurrentConfiguration != this.currentConfiguration
                    && this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentConfiguration"));
            }
        }

        [XmlElement("generatorServiceConfiguration")]
        public GeneratorServiceConfiguration GeneratorServiceConfiguration
        {
            get
            {
                return this.generatorServiceConfiguration;
            }
            set
            {
                this.generatorServiceConfiguration = value;
            }
        }

        #endregion

        #region Class Methods (Public)

        public static void Save(WcfClientGeneratorConfig config)
        {
            string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string configPath = Path.Combine(location, CONFIG_FILE_NAME);
            string backupPath = Path.Combine(location, Path.GetFileNameWithoutExtension(CONFIG_FILE_NAME) + ".bak");
            if (File.Exists(configPath))
                File.Copy(configPath, backupPath, true);
            SerializationManager.XmlSerialize(configPath, config);
        }

        #endregion
    }
}
